package com.youbang.bsMgr.role.controller;


import com.youbang.bsMgr.role.model.RoleEntity;
import com.youbang.bsMgr.role.service.RoleService;
import com.youbang.common.RtPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    protected static Logger logger=LoggerFactory.getLogger(RoleController.class);

    @RequestMapping("/GetAllRole")
    public String GetAllRole(String searchRoleName ,Model model){
        return "bsMgr/role/roleList";
    }
    @RequestMapping(value = "/GetAjaxRoleList",method = RequestMethod.POST)
    @ResponseBody
    public RtPageInfo GetAjaxRoleList(@ModelAttribute(value = "pageSize") Integer pageSize,
                                      @ModelAttribute( value = "startIndex") Integer startIndex,
                                      @ModelAttribute(value = "pageIndex") Integer pageIndex,
                                      @ModelAttribute("searchRoleName") String searchRoleName){
        //这里做查询操作
        List<RoleEntity> roleList = roleService.GetAllRole(pageSize,startIndex,pageIndex,searchRoleName);
        int allCout = roleService.GetAllRoleCout(searchRoleName);
        //返回
        RtPageInfo pageInfo = new RtPageInfo();
        pageInfo.setData(roleList);//这里是数据内容 List
        pageInfo.setPageNum(startIndex/pageSize);//Integer
        pageInfo.setPageSize(pageSize);//pageSize
        pageInfo.setTotalCount(allCout);//BigInteger
        return pageInfo ;
    }
    @RequestMapping(value = "/toAdd" )
    public String ToAdd(){
        return "bsMgr/role/roleAdd";
    }
    @RequestMapping(value = "/Add",method = RequestMethod.POST)
    public String InsertRole(RoleEntity roleEntity){
         roleService.InsertRole(roleEntity);
        return "redirect:/role/GetAllRole";
    }
    @RequestMapping(value = "/toUpdate" ,method = RequestMethod.POST)
    public String GetAllRole(int roleId, Model model){
        RoleEntity roleEntity = roleService.selectByPrimaryKey(roleId);
        model.addAttribute("role",roleEntity);
        return "bsMgr/role/roleEdit";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String Update(@ModelAttribute(value = "roleEntity") RoleEntity roleEntity){

        int result = roleService.updateByPrimaryKey(roleEntity);
        return "redirect:/role/GetAllRole";
    }
   @RequestMapping("/delete")
    public String Test(int roleId){
       int result = roleService.deleteByPrimaryKey(roleId);
        return "redirect:/role/GetAllRole";
    }
}
